// search模块的小仓库
import {reqCategoryList,reqGetBanner2List,reqGetCommodityList} from '@/api'
import axios from 'axios'

const state ={
    categoryList:[],
    banner2List:[],
    commodityList:[],
    goodsList:[],
    allgoodspublist:[]
}
const mutations={
    GATEGORYLIST(state,value){
        state.categoryList = value.slice(0,16)
    },
    GATBANNER2LIST(state,value){
        state.banner2List = value
    },
    GATCOMMODITYLIST(state,value){
        state.commodityList = value
    },
    GATALLGOODS(state,value){
        state.goodsList = value
    },
    GATALLGOODSPUB(state,value){
        state.allgoodspublist = value
    }
}
const actions={
    // 通过api里面的函数调用，获取服务器的数据
    async categoryList({commit}){
        let result = await reqCategoryList();
        console.log(result)
        if(result.code === 200){
            commit("GATEGORYLIST",result.data)
        }
    },
    async getBanner2List({commit}){
        let result = await reqGetBanner2List();
        if(result.code === 200){
            commit("GATBANNER2LIST",result.data)
            // console.log(result.data)
        }
    },
    // 获取商品信息
    async getCommodityList({commit}){
        let result = await reqGetCommodityList();
        // console.log(result)
        if(result.code === 200){
            commit("GATCOMMODITYLIST",result.data)
            // console.log(result.data)
        }
    },
    // 获取数据库里的商品信息
    getGoodsList({commit}){
        axios.get("http://localhost:8080/api/getAllGoods/getAllGoodslist",{
            params:{
              id:1
            }
          }).then(
            res=>{
              let result = res.data
              console.log("aa",result)
              if(res.status == 200){
                  commit("GATALLGOODS",result)
              }
              console.log("a",res)
  
            },
            error=>{
              console.log("b",error.message)
            }
          )
    },
    // 获取拍卖商品的时间
    getAllGoodsPublist({commit}){
        axios.get("http://localhost:8080/api/getAllGoodspub/getAllGoodspublist",{
          params:{
            id:1
          }
        }).then(
          res=>{
            let result = res.data
            if(res.status == 200){
                for(var i= 0;i<result.length;i++){
                    var startTemp = Number(String(result[i].startTime).slice(11,13))+8
                    var endTemp = Number(String(result[i].startTime).slice(11,13))+8
                    var startHour = startTemp%24 >= 10? startTemp%24 :'0'+startTemp%24
                    var endHour = endTemp%24 >= 10? endTemp%24 :'0'+endTemp%24
                    result[i].startTime = String(result[i].startTime).slice(0,10)+ ' ' + startHour + String(result[i].startTime).slice(13,19)
                    result[i].endTime = String(result[i].endTime).slice(0,10)+ ' ' + endHour + String(result[i].endTime).slice(13,19)
                }
                commit("GATALLGOODSPUB",result)
            }

          },
          error=>{
            console.log("88",error.message)
          }
        )
    },
}
const getters={
    classify(state){
        let classify =[]
        var allgoods = []
        // 2. 遍历对象属性，添加属性到另一个对象中
        for(var i=0;i<state.goodsList.length;i++){
            Object.assign(state.goodsList[i], state.allgoodspublist[i]);
        }
        allgoods = state.commodityList.concat(state.goodsList)
        let classifyAll = allgoods||[]
        classifyAll.forEach((item,index)=>{
            let {goodsCategory} = item
            let id=index
            if(!classify[goodsCategory]){
                classify[goodsCategory] = {
                    goodsCategory,
                    id,
                    child:[]
                }
            }
            classify[goodsCategory].child.push(item)
        })
        // 将分类好的数据转换，否则length为0
        let list = Object.values(classify)
        // console.log("整理之后的值为",list)
        return list
    }
}

export default{
    state,
    mutations,
    actions,
    getters
}